//
//  XHTopicModel.m
//  BSNoAnswer
//
//  Created by Tiffany宝 on 16/5/15.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

#import "XHTopicModel.h"
#import "NSDate+XHExtension.h"

@implementation XHTopicModel{
    //解决方法: 自己手动生成带 "_" 的成员变量
    CGFloat _cellHeight;
}

// get 方法中不能用 self.自己的属性 ,不然会死循环
-(NSString *)create_time{
    NSDateFormatter *fm = [NSDateFormatter new];
    fm.locale = [NSLocale localeWithLocaleIdentifier:@"en"];
    fm.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//    NSDate *creatDate = [fm dateFromString:self.create_time];
     NSDate *creatDate = [fm dateFromString:_create_time];
    NSDateComponents *cmps = [creatDate intervalToNow];
    //    XHLog(@"%@",[creatDate intervalToNow]);
    if ([creatDate isThisYear]) {
        if ([creatDate isToday]) {
            if (cmps.hour >= 1) {
                return [NSString stringWithFormat:@"%zd小时前",cmps.hour];
            }else if (cmps.minute >= 1){
                return [NSString stringWithFormat:@"%zd分钟前",cmps.minute];
            }else{
                return @"刚刚";
            }
        }else if([creatDate isYesterday]){
            fm.dateFormat = @"昨天 HH:mm:ss";
            return [fm stringFromDate:creatDate];
        }else{
            fm.dateFormat = @"MM-dd HH:mm:ss";
            return [fm stringFromDate:creatDate];
        }
    }else{
        return _create_time;
    }
}

// MJExtension --> 通过自定义的名字去替换网络请求来的数据名字
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    
    return @{
             @"smallImage":@"image0",
             @"largeImage":@"image1",
             @"bmiddleImage":@"image2"
             };
}

-(CGFloat)cellHeight{
    
//    XHLog(@"-----\n%@\n%@\n%@\n------\n",self.smallImage,self.largeImage,self.bmiddleImage);
    //判断~保证每个cell只加载一次
    if (!_cellHeight) {
        // *** 此方法已经过期
        //    CGFloat textH = [model.text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(XHScreenW - 2 * XHTopicViewCellMargin, CGFLOAT_MAX)].height;
        // 替换成 以下的方法
        CGSize maxSize = CGSizeMake(XHScreenW - 4 * XHTopicViewCellMargin, CGFLOAT_MAX);
        CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
        _cellHeight = textH + XHTopicViewCellTopH + XHTopicViewCellBottomH + 4 * XHTopicViewCellMargin;
//        XHLogFunc;
        if (self.type == XHTopicViewTypePicture) { // 图片帖子
//            XHLog(@"%f-----%f\n",self.width,self.height);
            
            CGFloat picViewW = maxSize.width; // 文字最大宽度
            CGFloat picViewH = self.height * picViewW / self.width;
            if (picViewH > XHPicViewMaxH) { // 长图
                picViewH = XHPicViewBreakH;
                //是大图
                self.bigImage = YES;
            }
            
            CGFloat picViewY = textH + XHTopicViewCellTopH + 2 * XHTopicViewCellMargin;
            CGFloat picViewX = XHTopicViewCellMargin;
            //在这里直接把图片帖子的frame一并设置了
            self.picViewFrame = CGRectMake(picViewX, picViewY, picViewW, picViewH);
            _cellHeight += picViewH;
        }else if (self.type == XHTopicViewTypeVoice){
            
            CGFloat voiceViewW = maxSize.width; // 文字最大宽度
            CGFloat voiceViewH = self.height * voiceViewW / self.width;
        
            CGFloat voiceViewY = textH + XHTopicViewCellTopH + 2 * XHTopicViewCellMargin;
            CGFloat voiceViewX = XHTopicViewCellMargin;
            //在这里直接把图片帖子的frame一并设置了
            self.voiceViewFrame = CGRectMake(voiceViewX, voiceViewY, voiceViewW, voiceViewH);
            _cellHeight += voiceViewH;
        }else if (self.type == XHTopicViewTypeVideo){
            CGFloat videoViewW = maxSize.width; // 文字最大宽度
            CGFloat videoViewH = self.height * videoViewW / self.width;
            
            CGFloat videoViewY = textH + XHTopicViewCellTopH + 2 * XHTopicViewCellMargin;
            CGFloat videoViewX = XHTopicViewCellMargin;
            //在这里直接把图片帖子的frame一并设置了
            self.videoViewFrame = CGRectMake(videoViewX, videoViewY, videoViewW, videoViewH);
            _cellHeight += videoViewH;

        }
//        XHLog(@"%f",_cellHeight);
    }
    return _cellHeight;
}

@end
